<?php
if (!defined('VALIDADMIN')) die ('Access Denied.');

/* 插件的基本信息 */
if (isset($set_plugins) && $set_plugins == TRUE)
{
	/**
	 *
	 * 插件的基本信息
	 * @var unknown_type
	 */
	$plugin=array(
	title=>'本地webservice接口',
	name=>'localservice',
	author=>'kwor',
	desc=>'支持所有webservice接口（包括GSM MODEL开发的接口）',
	admin=>'admin',
	type=>'serverSMS'
	);
}else {
	class localservice{
		//参数是pluginManager的引用
		function __construct(&$pluginManager){
			//注册指定方法
			//$mehod=array('admin','adminin','send');
			$mehod=get_class_methods(get_class());//注册该类所有方法
			foreach ($mehod as $value) {
				$pluginManager->register(get_class(),$this,$value);
			}
		}

		function admin() {
			$sql="select parm from sn_plugins where name = '".get_class()."'";
			$res=$GLOBALS[db]->getrow($sql);

			$jres=json_decode($res['parm'],true);//将json格式转换为数组
			//print_r($jres) ;

			$strcof='<form name="form1" method="post" action="">';

			$strcof.='<lable>短信API：</lable><input name="serverURL" type="text"
			size="100"  value="'.$jres['serverURL'].'" id="serverURL" /><br/><br/>';

			$strcof.='<lable>用户名：</lable><input name="servername" type="text"
			size="50"  value="'.$jres['servername'].'" id="servername" /><br/><br/>';

			$strcof.='<lable> 密&nbsp;&nbsp;&nbsp;&nbsp;码：</lable><input name="serverpass" type="text"
			size="50"  value="'.$jres['serverpass'].'" id="serverpass" /><br/><br/>';

/*
			$strcof.='<lable> 接口类型：</lable>
			 <select name="servertype">';
			if($jres['servertype']==0){
				$strcof.='<option value="0">webservice</option>
			<option value="1">Http</option>';
			}elseif($jres['servertype']==1){
				$strcof.='<option value="1">Http</option>
				<option value="0">webservice</option>';
			}
			$strcof.='</select><br/><br/>';
	*/		
			
			

			/*
			 $strcof.='<lable> 手续费：</lable><input name="pay_fee" type="text"
			 size="50"  value="'.$jres['pay_fee'].'" id="pay_fee" /><br/><br/>';
			 */
			$strcof.='<input type="hidden" name="action" value="admin" />';
			$strcof.='<input type="hidden" name="admindo" value="adminin" />';
			//$strcof.='<input type="hidden" name="name" value="'.$_REQUEST['name'].'" />';
			$strcof.='<input type="submit" name="Submit" value=" 确定 " />';
			$strcof.='</form>';
			return $strcof;

		}

		function adminin($date) {
			$plus=array();
			$plus['serverURL']=$_REQUEST['serverURL'];
			$plus['servername']=$_REQUEST['servername'];
			$plus['serverpass']=$_REQUEST['serverpass'];
		//	$plus['servertype']=$_REQUEST['servertype'];

			$sql="update sn_plugins set parm='".json_encode($plus)."' where name = '".get_class()."'";
			$res=$GLOBALS[db]->query($sql);
			if($res){
				echo '<script type="text/javascript">window.history.go(-1);</script>';
			}else{
				return  '设置失败';
			}

		}

		/**
		 *
		 * 短信发送方法
		 * @param 外部数据 $date （http接口为$date=array(mobile=>'手机号码',msg=>'消息内容'),webserverce接口参数为$date=array(mobile=>'手机号码',msg=>'消息内容',method=>'这里是调用的webservice方法')）
		 */
		function send($date) {
			$sql="select parm from sn_plugins where name = '".get_class()."'";
			$res=$GLOBALS[db]->getrow($sql);
			$jres=json_decode($res['parm'],true);//将json格式转换为数组(这里获取到短信接口的设置信息)
			
			if (class_exists('soapclient')) {
			//echo '存在soap类';
			
			try {

    //$client = new SoapClient("HelloService.wsdl",array('encoding'=>'UTF-8'));

    $client = new SoapClient($jres['serverURL'],array('encoding'=>'UTF-8'));

   $parameters =  array('user' => $jres['servername'],'pass' => $jres['serverpass'],'mobile' => $date['mobile'],'msg' => $date['msg']);
   //print_r($param);
   $res=$client->SMSsend($parameters);
   //return $res['SMSsendResult'] ;
   $rex=get_object_vars($res);
   return $rex['SMSsendResult'];
    //print_r($res);
	} catch (SOAPFault $e) {
    print $e;
	}

			}else{
			require_once '../../admin/inc/lib/base/cls_nusoap.php';
			//$client = new soapclient('http://218.92.182.147/sms/smsservice.asmx?wsdl',true);
			$client = new soapclient($jres['serverURL'],true);
			$client->soap_defencoding = 'UTF-8';
			$client->decode_utf8 = false;
			//$parameters = array('user' => "kwor",'pass' => "393345333",'mobile' => "13179323210",'msg' => "测试中文");
			$parameters = array('user' => $jres['servername'],'pass' => $jres['serverpass'],'mobile' => $date['mobile'],'msg' => $date['msg']);
			//注意参数
			$res=$client->call('SMSsend',$parameters);
			//*/
			if (!$err=$client->getError()) {
				//print_r($res); 
				return $res['SMSsendResult'] ;
			} else {
				echo " 错误 :",$err;
			}
			}
			//下面显示request和response 变量的内容
			/*
			 echo '<p/>';
			 echo 'Request:';
			 echo '<pre>',htmlspecialchars($client->request,ENT_QUOTES),'</pre>';
			 echo 'Response:';
			 echo '<pre>',htmlspecialchars($client->response,ENT_QUOTES ),'</pre>';
			*/



		}


	}
}
?>